package com.android.inestpin.adapt;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v4.app.FragmentActivity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.inestpin.R;
import com.android.inestpin.activity.BaseObject;
import com.android.inestpin.model.group.MainList;
import com.android.inestpin.model.group.MainThematicActivity_Insert;
import com.android.inestpin.utils.ConstantsUrl;
import com.android.inestpin.utils.ObjectUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mcxtzhang.nestlistview.NestFullListView;
import com.mcxtzhang.nestlistview.NestFullListViewAdapter;
import com.mcxtzhang.nestlistview.NestFullViewHolder;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/8/10.
 */
public class MainlistAdapt extends BaseMultiItemQuickAdapter<MainList.MainListdata.Mainlistdata, BaseViewHolder> {
    private String type;
    private ArrayList<MainThematicActivity_Insert.DataBean.ListBean> ta_insert;
    private FragmentActivity mcontext;
    private int limitposition;
    private int datasize;
    public MainlistAdapt(FragmentActivity context, ArrayList<MainThematicActivity_Insert.DataBean.ListBean> ta_insert, List<MainList.MainListdata.Mainlistdata> data, String type) {
        super(data);
      //  this.datasize = data.size();
        this.mcontext = context;
        this.type = type;
        this.ta_insert = ta_insert;
        addItemType(MainList.MainListdata.Mainlistdata.ONE, R.layout.item_list_main);
        addItemType(MainList.MainListdata.Mainlistdata.DOUBLE, R.layout.item_grid_detail_like);
    }
    public MainlistAdapt(Context context, List<MainList.MainListdata.Mainlistdata> data, String type) {
        super(data);
       // this.datasize = data.size();
        this.type = type;
        addItemType(MainList.MainListdata.Mainlistdata.ONE, R.layout.item_list_main);
        addItemType(MainList.MainListdata.Mainlistdata.DOUBLE, R.layout.item_grid_detail_like);
    }
    public ArrayList<MainThematicActivity_Insert.DataBean.ListBean> getTa_insert() {
        return ta_insert;
    }

    public void setTa_insert(ArrayList<MainThematicActivity_Insert.DataBean.ListBean> ta_insert) {
        this.ta_insert = ta_insert;
        limitposition = 4*ta_insert.size();
    }

    @Override
    protected void convert(BaseViewHolder holder, MainList.MainListdata.Mainlistdata mainlistdata) {
        switch (holder.getItemViewType()) {
            case MainList.MainListdata.Mainlistdata.DOUBLE:
                ((TextView) holder.getView(R.id.item_grid_detail_like_number_before)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                holder.getView(R.id.item_grid_open).setVisibility(View.GONE);
                holder.getView(R.id.item_grid_detail_like_number_before).setVisibility(View.GONE);
                holder.setText(R.id.item_grid_detail_like_title, mainlistdata.getName());
                holder.setText(R.id.item_grid_detail_like_number_after, "￥" + mainlistdata.getTeam_price());
//                holder.setText(R.id.item_grid_detail_like_number_before, "￥" + mainlistdata.getPrice());
                String sellNum = mainlistdata.getTeam_people_num();
                if (!TextUtils.isEmpty(sellNum) && !sellNum.equals("0")) {
                    holder.setText(R.id.item_grid_detail_like_tuanshu, "已团" + sellNum + "件");
                }
                String load = mainlistdata.getImg_src();
                if(TextUtils.isEmpty(load)){
                    load = mainlistdata.getThumb();
                }
                ObjectUtils.photo2(mContext, load, ((ImageView) holder.getView(R.id.item_grid_detail_like_img)));
                break;
            case MainList.MainListdata.Mainlistdata.ONE:
                ((TextView) holder.getView(R.id.item_list_sort_aa_before)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                ObjectUtils.photo2(mContext, mainlistdata.getThumb(), ((ImageView) holder.getView(R.id.main_list_item_image)));
                holder.setText(R.id.main_list_item_count, mainlistdata.getDiscount_price() + "折");
                holder.setText(R.id.main_list_item_tuannumber, mainlistdata.getTeam_num() + "人团");
                holder.setText(R.id.ll_start_soon_num, mainlistdata.getTeam_num() + "人团");
                holder.setText(R.id.ll_free_num, mainlistdata.getTeam_num() + "人团");
                String sellNum2 = mainlistdata.getTeam_people_num();
                if (!TextUtils.isEmpty(sellNum2) && !sellNum2.equals("0")) {
                    holder.setText(R.id.main_list_item_alltuannumber2, "已团" + sellNum2 + "件");
                }
                String price1 = mainlistdata.getTeam_price();
                SpannableString msp = new SpannableString("￥" + price1);
                msp.setSpan(new AbsoluteSizeSpan(13, true), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                holder.setText(R.id.main_list_item_money_after, msp);
                int endtime = Integer.parseInt(mainlistdata.getEnd_time());
                final int starttime = Integer.parseInt(mainlistdata.getStart_time());
                final int run_time = mainlistdata.getRun_time();
                holder.setText(R.id.main_list_item_title, mainlistdata.getName());
//                switch (mainlistdata.getTypeid()) {
//                    case "1":
//                        holder.setText(R.id.title_type, "拼团");
//                        holder.setText(R.id.main_list_item_title, "             " + mainlistdata.getName());
//                        break;
//                    case "2":
//                        holder.setText(R.id.title_type, "新人专享");
//                        holder.setText(R.id.main_list_item_title, "                     " + mainlistdata.getName());
//                        break;
//                    case "3":
//                        holder.setText(R.id.title_type, "限时秒杀");
//                        holder.setText(R.id.main_list_item_title, "                     " + mainlistdata.getName());
//                        break;
//                    case "4":
//                        holder.setText(R.id.title_type, "限时秒杀");
//                        holder.setText(R.id.main_list_item_title, "                     " + mainlistdata.getName());
//                        break;
//                    case "5":
//                        holder.setText(R.id.title_type, "免费试用");
//                        holder.setText(R.id.main_list_item_title, "                     " + mainlistdata.getName());
//                        break;
//                    case "6":
//                        holder.setText(R.id.title_type, "抽奖");
//                        holder.setText(R.id.main_list_item_title, "             " + mainlistdata.getName());
//                        break;
//                    case "7":
//                        holder.setText(R.id.title_type, "AA团");
//                        holder.setText(R.id.main_list_item_title, "             " + mainlistdata.getName());
//
//                        break;
//                    case "8":
//                        holder.setText(R.id.title_type, "推广团");
//                        holder.setText(R.id.main_list_item_title, "                 " + mainlistdata.getName());
//
//                        break;
//                    case "9":
//                        holder.setText(R.id.title_type, "自选团");
//                        holder.setText(R.id.main_list_item_title, "                 " + mainlistdata.getName());
//                        break;
//                    case "10":
//                        holder.setText(R.id.title_type, "阶梯团");
//                        holder.setText(R.id.main_list_item_title, "                 " + mainlistdata.getName());
//                        break;
//                }

                String strendTime;
                switch (type) {
                    //免费试用
                    case "2":
                        holder.setText(R.id.main_list_item_count, "试用");
                        holder.setVisible(R.id.item_list_sort_lottory_time, true);
                        strendTime = ObjectUtils.getStrTime(mainlistdata.getEnd_time(), "MM月dd日 HH:mm");
                        holder.setText(R.id.item_list_sort_lottory_time, "截止时间" + strendTime);
                        holder.setVisible(R.id.main_list_item_stock, true);
                        if (run_time > endtime && run_time > starttime) {
                            holder.setText(R.id.main_list_item_stock, "活动已经结束");
                            holder.setVisible(R.id.ll_result, true);
                            holder.setVisible(R.id.ll_free, false);
                            holder.setVisible(R.id.ll_start_soon, false);
                        } else if (run_time > starttime && run_time < endtime) {
                            holder.setText(R.id.main_list_item_stock, "限量" + mainlistdata.getLuck_num() + "件");
                            holder.setVisible(R.id.ll_result, false);
                            holder.setVisible(R.id.ll_free, true);
                            holder.setVisible(R.id.ll_start_soon, false);
                        } else if (run_time < starttime) {
                            holder.setText(R.id.main_list_item_stock, "限量" + mainlistdata.getLuck_num() + "件");
                            holder.setVisible(R.id.ll_result, false);
                            holder.setVisible(R.id.ll_free, false);
                            holder.setVisible(R.id.ll_start_soon, true);
                        }
                        holder.setText(R.id.main_list_item_alltuannumber2, "￥" + mainlistdata.getPrice());
                        ((TextView) holder.getView(R.id.main_list_item_alltuannumber2)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                        break;
                    //抽奖团
                    case "5":
                        holder.setText(R.id.main_list_item_count, "抽奖");
                        holder.setVisible(R.id.item_list_sort_lottory_time, true);
                        strendTime = ObjectUtils.getStrTime(mainlistdata.getEnd_time(), "MM月dd日 HH:mm");
                        holder.setText(R.id.item_list_sort_lottory_time, "截止时间" + strendTime);
                        if (run_time > endtime && run_time > starttime) {
                            holder.setVisible(R.id.ll_result, true);
                            holder.setVisible(R.id.main_list_item_go_kai, false);
                            holder.setVisible(R.id.ll_start_soon, false);
                        } else if (run_time > starttime && run_time < endtime) {
                            holder.setVisible(R.id.ll_result, false);
                            holder.setVisible(R.id.main_list_item_go_kai, true);
                            holder.setVisible(R.id.ll_start_soon, false);
                        } else if (run_time < starttime) {
                            holder.setVisible(R.id.ll_result, false);
                            holder.setVisible(R.id.main_list_item_go_kai, false);
                            holder.setVisible(R.id.ll_start_soon, true);
                        }
                        holder.setText(R.id.main_list_item_alltuannumber2, "￥" + mainlistdata.getPrice());
                        ((TextView) holder.getView(R.id.main_list_item_alltuannumber2)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                        break;
                    //AA团
                    case "6":
                        holder.setVisible(R.id.ll_price_aa, true);
                        holder.setVisible(R.id.main_list_item_go_kai, true);
                        holder.setVisible(R.id.ll_price_normal, false);
                        SpannableString msp_aa = new SpannableString("￥" + mainlistdata.getTeam_price());
                        msp_aa.setSpan(new AbsoluteSizeSpan(13, true), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        holder.setText(R.id.item_list_sort_aa_total, msp_aa);
                        holder.setText(R.id.item_list_sort_aa_before, "￥" + mainlistdata.getPrice());
                        holder.setText(R.id.item_list_sort_aa_tuanshu2, mainlistdata.getTeam_num() + "人团总价:");
                        double v = Double.parseDouble(price1);
                        int tuan = Integer.parseInt(mainlistdata.getTeam_num());
                        String price = ObjectUtils.div(v, tuan, 2) + "";
                        String[] split = price.split("\\.");
                        if (split[1].equals("0")) {
                            price = split[0];
                        }
                        holder.setText(R.id.item_list_sort_aa_after, "每人" + price + "元");
                        break;
                    default:
                        holder.setVisible(R.id.main_list_item_go_kai, true);

                        break;

                }
                /**
                 * 插入专题活动
                 */
               final int taposition=holder.getAdapterPosition();
                if(mcontext instanceof FragmentActivity) {
                    if (taposition % 4 == 0 && taposition <= limitposition) {

                        holder.getView(R.id.ll_tainsert).setVisibility(View.VISIBLE);
                        holder.getView(R.id.progress_hot).setVisibility(View.INVISIBLE);
                       TextView  more_tv = (TextView)holder.getView(R.id.more_tv);
                        TextView hotactivity_title = (TextView)holder.getView(R.id.hotactivity_title);
                        hotactivity_title.setText(ta_insert.get(taposition / 4 - 1).getName());
                        //水平滚动列表
                        NestFullListView tabrow = (NestFullListView) holder.getView(R.id.grid);
                        //        //动态设置选项卡宽度，超过2个可以滑动查看
                        DisplayMetrics metrics = new DisplayMetrics();
                        mcontext.getWindowManager().getDefaultDisplay().getMetrics(metrics);
                        int width = (int) (metrics.widthPixels / 2.5);
                        final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT);
                        tabrow.setAdapter(new NestFullListViewAdapter<MainThematicActivity_Insert.DataBean.ListBean.ApplyBean>(R.layout.list_main_hor_adapter,ta_insert.get(taposition / 4 - 1).getApply_list()) {
                            @Override
                            public void onBind(int position, MainThematicActivity_Insert.DataBean.ListBean.ApplyBean testBean, NestFullViewHolder holder) {
                                (holder.getView(R.id.item_grid_detail_like_ll)).setLayoutParams(params);
                                ((TextView) holder.getView(R.id.item_grid_detail_like_number_before)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                                String price1 = testBean.getTeam_price();
                                SpannableString msp = new SpannableString("￥" + price1);
                                msp.setSpan(new AbsoluteSizeSpan(10, true), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                                ((TextView) holder.getView(R.id.item_grid_detail_like_number_after)).setText(msp);
                                holder.setText(R.id.item_grid_detail_like_number_before, "￥" + testBean.getPrice());
                                holder.setText(R.id.item_grid_detail_like_title, testBean.getName());
                                ObjectUtils.photo2(mcontext, testBean.getImg_cover(), ((ImageView) holder.getView(R.id.item_grid_detail_like_img)));

                            }
                        });

                        tabrow.setOnItemClickListener(new NestFullListView.OnItemClickListener() {
                            @Override
                            public void onItemClick(NestFullListView nestFullListView, View view, int pos) {
                                 ObjectUtils.ToDetailActivity(mcontext, 1, ta_insert.get(taposition / 4 - 1).getApply_list().get(pos).getGoods_id(), "");
                            }
                        });
                        more_tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(mcontext, BaseObject.class);
                                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_NEED_ID, ta_insert.get(taposition / 4 - 1).getId());
                                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE,"专题活动");
                                mcontext.startActivity(intent);
                            }
                        });

                    }
                    else
                    {
                        holder.getView(R.id.ll_tainsert).setVisibility(View.GONE);
                    }
                }

                holder.addOnClickListener(R.id.main_list_item_go_kai)
                        .addOnClickListener(R.id.ll_free)
                        .addOnClickListener(R.id.ll_start_soon)
                        .addOnClickListener(R.id.ll_result);


                break;
        }

    }


}
